package com.dongdongshop.aspect;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/*配置事务*/
@Aspect  //表示一个切面类
@Component
public class LogAop {
    Logger logger = LoggerFactory.getLogger(LogAop.class);

    @Pointcut("execution(public * com.dongdongshop.service.*.*(..))")
    public void aopLog(){//切点表达式
    }

    @Before("aopLog()")
    public void logBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();//获取全部参数
        Signature signature = joinPoint.getSignature();
        logger.info("调用service中的{}方法成功,参数为:[{}]",signature,args);
    }

    /*返回后通知*/
    @AfterReturning(pointcut = "aopLog()",returning = "ret")
    public void logAfter(Object ret) throws Throwable{//ret返回值,声明异常
        /*将Object类型转为json格式,先引入依赖 <artifactId>fastjson</artifactId>*/
        logger.info("方法执行成功,返回值为:{}", JSONObject.toJSONString(ret));
    }
}
